var params = null;
var cashBindId = null;
var orderNum = null;
var payerId = null;
var type = null;
var openId = null;
mui.init();
mui.ready(function () {
    cashBindId = getUrlParam("cashBindId");
    orderNum = getUrlParam("orderNum");
    payerId = getUrlParam("payerId");
    type = getUrlParam("type");
    openId = getUrlParam("openId");
    // var url = "http://akk.s1.natapp.cc/thmlfApi/avs/hkpay/createorder";
    var url = "http://www.028wkf.cn/thmlfApi/avs/hkpay/createorder";
    $.ajax({
        url: url,
        type: "POST",
        dataType: "JSON",
        data: JSON.stringify({
            "cashBindId": cashBindId,
            "orderNum": orderNum,
            "payerId": payerId,
            "openId": openId,
            "type": type
        }),
        contentType: 'application/json',
        success: function (res) {
            // alert(JSON.stringify(res));
            if (res.code == 200) {
                params = res.result.payInfo;
                params = new Base64().decode(params);
                params = JSON.parse(params);
                // alert(JSON.stringify(params));
                wx.config({
                    debug: false,
                    appId: params.appId,
                    timestamp: params.timestamp,// 签名时间戳
                    nonceStr: params.nonceStr,  // 签名随机字符串
                    signature: params.signType, // 签名
                    jsApiList: ['chooseWXPay', "closeWindow"]
                });
            } else {
                mui.toast(res.message);
            }
        },
        error: function (e) {
            mui.toast("服务器繁忙");
        }
    });
});

wx.ready(function () {
    onBridgeReady(params);
});
wx.error(function (res) {
    // mui.alert("信息验证失败");
    console.error("config error", res);
});

function onBridgeReady(data) {
    WeixinJSBridge.invoke(
        'getBrandWCPayRequest', data,
        function (res) {
            if (res.err_msg == "get_brand_wcpay_request:ok") {
                $('.pay_success').css('display', 'block');
                $('.pay_cancel').css('display', 'none');
                $('.pay_loading').css('display', 'none');
            } else {
                // alert(res.err_desc);
                $('.pay_success').css('display', 'none');
                $('.pay_cancel').css('display', 'block');
                $('.pay_loading').css('display', 'none');
            }    // 使用以上方式判断前端返回,微信团队郑重提示：res.err-msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。
        }
    );
    if (typeof WeixinJSBridge == "undefined") {
        if (document.addEventListener) {
            document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
        } else if (document.attachEvent) {
            document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
            document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
        }
    } else {
//        alert('s');
//         onBridgeReady(data);
    }
}